<?php
	/*
	file ini digunakan untuk mengontrol login apakah user telah sesuai prosedur masuk aplikasi
	*/
class pengumuman extends CI_Controller {

	function __construct(){
		parent::__construct();
		session_start();
		$this->load->model('m_pengumuman');

	}//end Login

	function get_action(){
		$task = isset($_POST['task']) ? @$_POST['task'] : $_GET['task'];
		switch($task){
			case "list":
				$this->pengumuman_list();
			break;
			case "create":
				$this->pengumuman_create();
			break;
			case "update":
				$this->pengumuman_update();
			break;
			case "delete":
				$this->pengumuman_delete();
			break;
			case "search":
				$this->pengumuman_search();
			break;
			default:
				echo "{ failure : true }";
			break;
		}
	}

	function index(){
		redirect(base_url());
	}

	function pengumuman_load(){
		$this->load->view('admin/v_pengumuman_list');
	}

	function pengumuman_addedit(){
		$data = array();
		$id = $this->uri->segment(3);
		if($id != false){
			$data['dataDetail'] = $this->getDetail($id);
		}
		$this->load->view('admin/v_pengumuman_post', $data);
	}

	function read(){
		//$_SESSION[SESSION_PAGE]=1;

		$url=$this->uri->segment(6);
		$url=str_replace('_',' ',$url);
		$url=str_replace('.html','',$url);
		$data["content"]=$this->m_pengumuman->get_view($url);
		$menulink='pengumuman/listview';
		$data["parentid"]=$this->m_public_function->get_menuparent_by_url($menulink);
		//$this->firephp->log($menulink);
		$this->load->view('v_berita_view',$data);
	}

	function listview(){

		$query = isset($_POST['query']) ? $_POST['query'] : "";

		$config['base_url'] = base_url().'pengumuman/listview';
		$config['total_rows'] = $this->m_pengumuman->pengumuman_get_count_rows($query);
		$config['per_page'] = '20';

		$this->pagination->initialize($config);


		$per_page = (integer) (isset($_GET['per_page']) ? $_GET['per_page'] : 0);
		$start = $per_page+1;
		$end = $config['per_page'];


		$data["title"]="PENGUMUMAN";
		$data["menus"]=$this->m_public_function->get_menu_home();
		$data["content"]=$this->m_pengumuman->pengumuman_listview($query, $start, $end);
		$data["page"]=$this->pagination->create_links();

		$url=$this->uri->uri_string();
		$data["parentid"]=$this->m_public_function->get_menuparent_by_url($url);

		$this->load->view('v_pengumuman_listview',$data);
	}

	function pengumuman_list(){
		$query = $this->input->post('query');
		$pagenum = $_GET['pagenum'];
		$pagesize = $_GET['pagesize'];
		$start = $pagenum * $pagesize;
		$end = $start + $pagesize;

		$result=$this->m_pengumuman->pengumuman_list($query, $start, $end);
		echo $result;

	}

	function pengumuman_save(){
		$id = $this->input->post('id');
		$kategori_id = $this->input->post('kategori_id');
		$kategori_id = is_numeric($kategori_id) ? $kategori_id : 0;
		$judul = $this->input->post('judul');
		$tanggal = $this->input->post('tanggal');
		$tag = $this->input->post('tag');
		$isi_pengumuman = $this->input->post('isi_pengumuman');
		$headlines = $this->input->post('headlines');
		$publish = $this->input->post('publish');
		$image_pengumuman = $this->input->post('image_pengumuman');
		$task = $this->input->post('task');
		$data = array(
			'judul'=>$judul,
			'created_date'=>$tanggal,
			'tag'=>$tag,
			'isi_pengumuman'=>$isi_pengumuman,
			'headlines'=>$headlines,
			'publish'=>$publish,
			'image_pengumuman'=>$image_pengumuman
		);
		if($kategori_id != 0){
			$data["kategori_id"]=$kategori_id;
		}
		$params = array(
			'id'=>$id,
			'data'=>$data,
			'task'=>$task
		);
		$result = $this->m_pengumuman->pengumuman_save($params);
		echo $result;
	}

	function getDetail($id){
		$sql = "
			SELECT
				pengumuman.id,
				judul,
				isi_pengumuman,
				pengumuman.created_date,
				tag,
				isi_pengumuman,
				headlines,
				publish,
				image_pengumuman,
				kategori_id,
				kategori
			FROM pengumuman
			JOIN kategori ON pengumuman.kategori_id = kategori.id
			WHERE pengumuman.id = ".$id."
		";
		$query = $this->db->query($sql);

		$num_rows = $query->num_rows();
		if($num_rows > 0){
			return $query->row();
		}
	}

	function pengumuman_delete(){
		$id = $this->input->post('id');
		$id = is_numeric($id) ? $id : $id;
		$result = $this->m_pengumuman->pengumuman_delete($id);
		echo $result;
	}

	function pengumuman_search(){

	}

	function pengumuman_update(){

	}

	function pengumuman_create(){

	}

}
?>
